﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;

namespace ASV.Account
{
    public partial class Benutzerbearbeiten : System.Web.UI.Page
    {
        private string UserId;
        private object[] Userdata;

        protected void Page_Init(object sender, EventArgs e)
        {
            this.UserId = Request.QueryString.Get("id");
            this.Userdata = this.getUserData(this.UserId);

            Vorname.Text = this.Userdata[1].ToString();
            Nachname.Text = this.Userdata[2].ToString();
            Email.Text = this.Userdata[3].ToString();
            Benutzergruppe.Text = this.Userdata[5].ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication auth = new Authentication();
            int userGroup;
            int allowedGroup;

            this.UserId = Request.QueryString.Get("id");

            if (auth.isLoggedIn() == true)
            {
                userGroup = int.Parse(HttpContext.Current.Session["benutzertyp"].ToString());
                allowedGroup = 3;
                if (auth.checkUserGroup(userGroup, allowedGroup) == false)
                {
                    Response.Redirect("~/Error.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Error.aspx");
            }

            this.Userdata = this.getUserData(this.UserId);
        }

        private bool checkEmail(string email)
        {
            Connection mycon = new Connection();
            MySqlCommand selectUser = new MySqlCommand();
            string SQL = "SELECT * FROM benutzer WHERE email = '" + email + "'";
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectUser);
            MySqlDataReader reader = selectUser.ExecuteReader();

            selectUser.CommandText = SQL;
            selectUser.Connection = mycon.getConnection();
            
            if (reader.HasRows == true)
            {
                return false;
            }
            return true;
        }

        private void updateUser(string vorname, string nachname, string email, string benutzergruppe)
        {
            Connection connection = new Connection();

            string SQL = "UPDATE `benutzer` SET `vorname` = '" + vorname + "', `nachname` = '" + nachname + "', `email` = '" + email + "', `benutzertyp` = '" + benutzergruppe + "' WHERE `kundennummer` = " + this.UserId;
            
           // connection.insert(SQL);
        }

        private object[] getUserData(string id)
        {
            Connection connection = new Connection();
            string SQL = "SELECT * FROM `benutzer` WHERE `kundennummer` = " + this.UserId;
            object[] test = null;

            //  return connection.select(SQL);
            return test;
        }

        protected void bearbeiten_Click(object sender, EventArgs e)
        {
            string vorname = Vorname.Text;
            string nachname = Nachname.Text;
            string email = Email.Text;
            string benutzergruppe = Benutzergruppe.Text;

            bool errorStatus = true;
            string errorText = "<ul>";

            if (email != this.Userdata[3].ToString())
            {
                if (this.checkEmail(email) == false)
                {
                    errorText += "<li><b>Email</b>: Diese E-Mail wird bereits verwendet</li>";
                    errorStatus = false;
                }
            }

            if (benutzergruppe == "auswählen")
            {
                errorText += "<li><b>Benutzergruppe:</b>Bitte wählen Sie eine Benutzergruppe aus</li>";
                errorStatus = false;
            }

            errorText += "</ul>";

            if (errorStatus == false)
            {
                errors.Text = errorText;
            }
            else
            {
                this.updateUser(vorname, nachname, email, benutzergruppe);
                
                errors.Text = "";
                erfolgsmeldung.Visible = true;
            }
        }
        
    }
}